const db = require("../db");

const noteService = {
  // 新增 留言/照片
  addNote: async (
    type,
    content,
    user_name,
    user_id,
    label,
    color,
    create_time,
    image_url,
    like_count,
    comment_count
  ) => {
    const sql = "INSERT INTO note SET ?";
    await db.query(sql, {
      type,
      content,
      user_name,
      user_id,
      label,
      color,
      create_time,
      image_url,
      like_count,
      comment_count,
    });
  },
  // 获取留言/照片列表
  getNoteList: async (type, sort, label, limit, page) => {
    if (label == "全部") {
      const sql = `SELECT * FROM note WHERE type = ? ORDER BY ${sort} DESC LIMIT ${limit} OFFSET ${
        (page - 1) * limit
      } `;
      return await db.query(sql, type);
    } else {
      const sql = `SELECT * FROM note WHERE type = ? AND label = ? ORDER BY ${sort} DESC LIMIT ${limit} OFFSET ${
        (page - 1) * limit
      } `;
      return await db.query(sql, [type, label]);
    }
  },
  // 获取留言/照片
  getNote: async (id) => {
    const sql = "SELECT * FROM note WHERE id = ?";
    return await db.query(sql, id);
  },
  // 删除留言/照片
  deleteNote: async (id) => {
    const sql =
      "DELETE a,b,c FROM note a LEFT JOIN feedback b ON a.id=b.wall_id LEFT JOIN comment c ON a.id = c.wall_id WHERE a.id = ?";
    await db.query(sql, id);
  },
  // 新增反馈
  addFeedback: async (wall_id, user_id, type, create_time, like) => {
    const sql = "INSERT INTO feedback SET ?";
    await db.query(sql, {
      wall_id,
      user_id,
      type,
      create_time,
    });
    if (like) {
      const sql1 = "UPDATE note SET like_count = ? WHERE id = ?";
      await db.query(sql1, [like, wall_id]);
    }
  },
  // 删除反馈
  deleteFeedback: async (user_id, count, wall_id) => {
    const sql = "DELETE FROM feedback WHERE user_id = ? AND wall_id = ?";
    await db.query(sql, [user_id, wall_id]);
    const sql1 = "UPDATE note SET like_count = ? WHERE id = ?";
    await db.query(sql1, [count, wall_id]);
  },
  // 获取反馈列表
  getFeedbackList: async (type) => {
    const sql = "SELECT * FROM feedback WHERE type = ?";
    return await db.query(sql, type);
  },
  // 新增评论
  addComment: async (
    wall_id,
    user_id,
    user_name,
    avatar_url,
    comment,
    create_time,
    count
  ) => {
    const sql = "INSERT INTO comment SET ?";
    await db.query(sql, {
      wall_id,
      user_id,
      user_name,
      avatar_url,
      comment,
      create_time,
    });
    const sql1 = "UPDATE note SET comment_count = ? WHERE id = ?";
    await db.query(sql1, [count, wall_id]);
  },
  // 获取评论列表
  getCommentList: async (wall_id, limit, page) => {
    const sql = `SELECT * FROM comment WHERE wall_id = ? ORDER BY create_time DESC LIMIT ${limit} OFFSET ${
      (page - 1) * limit
    } `;
    return await db.query(sql, wall_id);
  },
  // 删除评论
  deleteComment: async (id, wall_id, count) => {
    const sql = "DELETE FROM comment WHERE id = ?";
    await db.query(sql, id);
    const sql1 = "UPDATE note SET comment_count = ? WHERE id = ?";
    await db.query(sql1, [count, wall_id]);
  },
  // 是否反馈
  isFeedback: async (user_id, wall_id, type) => {
    const sql =
      "SELECT COUNT(*) AS count FROM feedback WHERE wall_id = ? AND user_id = ? AND type = ?";
    return await db.query(sql, [wall_id, user_id, type]);
  },
};

module.exports = noteService;
